package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.OutOrderMapper;
import com.kgc.service.IOutOrderProviderService;
import entity.Outorder;
import entity.Outorderinfo;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @description:OutOrderProviderSerivce 实现类
 * @author: Cheng
 * @create: 2022-04-20 11:10
 **/
@Service
public class IOutOrderProviderServiceImpl implements IOutOrderProviderService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private OutOrderMapper outOrderMapper;

    /**
     * 按条件查询门店信息
     * @param orderNum
     * @param sname
     * @param startTime
     * @param endTime
     * @param status
     * @param pageNum
     * @return
     */
    @Override
    public ResponseMessage selectOutOrderInfo(String orderNum, String sname, Date startTime, Date endTime,int status ,int pageNum) {
        logger.info("IOutOrderProviderServiceImpl selectOutOrderInfo is start ... params:orderNum:-->" + orderNum + ",sname:-->" + sname + ",startTime:-->" + startTime + ",endTime:-?" + endTime +",status:->"+status+ ",pageNum:->" + pageNum);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("orderNum", orderNum);
            map.put("sname", sname);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("status", status);
            PageHelper pageHelper = new PageHelper();
            pageHelper.startPage(pageNum, 5);
            List<Outorder> outorderList = outOrderMapper.selectOutOrderInfo(map);
            PageInfo pageInfo = new PageInfo(outorderList);
            logger.debug("IOutOrderProviderServiceImpl selectOutOrderInfo result is pageInfo" + pageInfo);
            if (outorderList == null) {
                logger.error("IOutOrderProviderServiceImpl selectOutOrderInfo is error ,ruselt size is null");
                responseMessage.setMessage("IOutOrderProviderServiceImpl selectOutOrderInfo is error");
                responseMessage.setCode(StatusCode.ERROR);
            } else {
                responseMessage.setMessage("查询订单信息成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(pageInfo);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            logger.error("IOutOrderProviderServiceImpl selectOutOrderInfo  is error", e);
            e.printStackTrace();
            responseMessage.setMessage("订单信息查询是出现一个系统错误");
            responseMessage.setCode(StatusCode.ERROR);
            return responseMessage;
        }
        return responseMessage;
    }

    /**
     * 通过OutOrderInfo的id查询    联表 product
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectProductAndOutOrderInfo(String id) {
        logger.info("IOutOrderProviderServiceImpl selectProductAndOutOrderInfo is start ... params:id"+id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
           List<Outorderinfo> outorderinfoList = outOrderMapper.selectProductAndOutOrderInfo(id);
            logger.debug("IOutOrderProviderServiceImpl selectProductAndOutOrderInfo result is outorderinfoList" + outorderinfoList);
            if (outorderinfoList.size() <=0) {
                logger.error("IOutOrderProviderServiceImpl selectOutOrderInfo is error ,result outorderinfo is null");
                responseMessage.setMessage("IOutOrderProviderServiceImpl selectProductAndOutOrderInfo is error");
                responseMessage.setCode(StatusCode.ERROR);
            } else {
                responseMessage.setMessage("通过orderInfo查询商品信息成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(outorderinfoList);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            logger.error("IOutOrderProviderServiceImpl selectProductAndOutOrderInfo  is error", e);
            e.printStackTrace();
            responseMessage.setMessage("商品信息查询是出现一个系统错误");
            responseMessage.setCode(StatusCode.ERROR);
            return responseMessage;
        }
        return responseMessage;
    }
}
